/**
 * @description ssr html 字符串
 * @author xfj
 */

import React from 'react';
import { renderToString } from 'react-dom/server';
import { StaticRouter, Route } from 'react-router-dom';
import { Provider } from 'react-redux';
import routes from '../share/route';

export default (req, store) => {

  const content = renderToString(
    <Provider store={store}>
      <StaticRouter location={req.path}>
        { routes.map(item => <Route key={item.path} {...item} />) }
      </StaticRouter>
    </Provider>
  );

  const list = JSON.stringify(store.getState().list);

  return `
    <!DOCTYPE html>
    <html>
      <head>
        <title>React SSR hello</title>
      </head>
      <body>
        <div id="root">${content}</div>
        <script>
          window.listContent = ${list}
        </script>
        <script src="index.js"></script>
      </body>
    </html>
  `;
}